package com.jia.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.dto.admin.CoordinateDto;
import com.jia.entity.Address;
import com.jia.entity.Coordinate;
import com.jia.entity.User;
import com.jia.mapper.AddressMapper;
import com.jia.service.CoordinateService;
import com.jia.mapper.CoordinateMapper;
import com.jia.vo.admin.CoordinateVo;
import com.jia.vo.admin.SitesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.SystemPropertyUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 贾天旺
* @description 针对表【t_coordinate】的数据库操作Service实现
* @createDate 2024-04-28 09:22:25
*/
@Service
public class CoordinateServiceImpl extends ServiceImpl<CoordinateMapper, Coordinate>
    implements CoordinateService{

    @Autowired
    private CoordinateMapper coordinateMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Page<CoordinateVo> COORDINATE_VO_PAGE(Integer page, Integer limit, CoordinateDto coordinateDto) {
        return this.coordinateMapper.COORDINATE_VOS(new Page<CoordinateVo>(page,limit),coordinateDto);
    }

    @Override
    public List<String> provinceList() {

        List<String> list = this.coordinateMapper.provinceList();
        return list;
    }

    @Override
    public List<String> cityList(String province) {
        return this.coordinateMapper.cityList(province);
    }

    @Override
    public List<String> areaList(String city) {
        return this.coordinateMapper.districtList(city);
    }

    @Override
    public void updateState(int id) {
        Address address = addressMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(address)) {
            address.setState(address.getState() == 1 ? 0 : 1);
            addressMapper.updateById(address);
        }
    }

    @Override
    public void deleteBattery(int id) {
        Coordinate coordinate = this.coordinateMapper.selectById(id);
        if(ObjectUtil.isNotEmpty(coordinate)){
            //删除地址
            int i = addressMapper.deleteById(coordinate.getAid());
            if(i>=1){
                //删除坐标
                this.coordinateMapper.deleteById(id);
            }
        }
    }

    @Override
    public List<SitesVo> mapList() {
        List<Coordinate> coordinates = this.coordinateMapper.list();
        List<SitesVo> list=new ArrayList<>();
        coordinates.forEach(item->{
            SitesVo sitesVo=new SitesVo();
            sitesVo.setId(item.getId());
            sitesVo.setX(item.getX());
            sitesVo.setY(item.getY());
            list.add(sitesVo);
        });
        return list;
    }

    @Override
    public void addBattrey(CoordinateDto coordinateDto) {
        //添加地址
        Address address=new Address();

        address.setProvince(coordinateDto.getProvince());

        address.setCity(coordinateDto.getCity());

        address.setDistrict(coordinateDto.getDistrict());

        address.setDescription(coordinateDto.getDescription());

        address.setState(1);

        address.setCreatetime(new Date());

        int i = addressMapper.insertAddress(address);
        //查询最大id
        Integer id = addressMapper.selectMaxId();

        if(i>=1){
            Coordinate coordinate=new Coordinate();
            System.out.println("id为===>"+id);
            coordinate.setAid(id);
            coordinate.setX(coordinateDto.getLongitude());
            coordinate.setY(coordinateDto.getLatitude());
            coordinate.setCreatetime(new Date());
            coordinate.setSixty(20);
            coordinate.setFortyFive(20);
            coordinate.setMaxSixty(20);
            coordinate.setMaxFortyFive(20);

            this.coordinateMapper.insert(coordinate);
        }
    }
}




